package com.hs.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.*;
import com.hs.sfzy.mapper.CourseMapper;
import com.hs.sfzy.mapper.CoursemarkMapper;
import com.hs.sfzy.mapper.UserCourseMapper;
import com.hs.sfzy.service.CourseService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: sfzyBackEnd
 * @BelongsPackage: com.hs.sfzy.service.impl
 * @ClassName CourseServiceImpl
 * @Author: 陈千鱼不是鱼
 * @CreateTime: 2024-10-24  14:34
 * @Description: 课程服务层
 * @Version: 1.0
 */
@Service
@RequiredArgsConstructor
public class CourseServiceImpl implements CourseService {


    @Autowired
    private final CourseMapper courseMapper;

    @Autowired
    private CoursemarkMapper coursemarkMapper;

    @Autowired
    private UserCourseMapper userCourseMapper;

    @Override
    public PageInfo<Course> page(CourseDTO courseDTO) {
        PageHelper.startPage(courseDTO.getPageNum(),courseDTO.getPageSize());
        List<Course> list = courseMapper.selectAll(courseDTO);
        PageInfo<Course> pageInfo = new PageInfo<Course>(list);
        return pageInfo;
    }

    @Override
    public void save(Course course) {
        int i =  courseMapper.save(course);
        if (i == 0){
            throw new RuntimeException("添加失败");
        }
    }

    @Override
    public void deleteById(Integer id) {
        int i = courseMapper.deleteById(id);
        if (i == 0){
            throw new RuntimeException("删除失败");
        }
    }

    @Override
    public void update(Course course) {
        int i = courseMapper.update(course);
        if (i == 0){
            throw new RuntimeException("更新失败");
        }
    }

    /**
     * 根据课程评分降序查询
     * @author QinKunPeng
     * @date 2024/10/28 14:21
     * @return java.util.List<com.hs.sfzy.entity.Course>
     */
//    @Override
//    public List<Coursemark> findByAvgGrade() {
//        return coursemarkMapper.selectByAvgGrade();
//    }

    public int updateGrade(int courseId){
        return courseMapper.updateCourseGrade(courseId);
    }

    @Override
    public List<Course> selectForWx(Map map) {
        return courseMapper.selectForWx(map);
    }

    @Override
    public Course selectById(Long id) {
        Course course = courseMapper.selectById(id);
        // 根据课程id获取课程的评价数
        Integer count = coursemarkMapper.countCoursemarkByCourseId(id);
        Integer count2 = userCourseMapper.countUserCourseByCourseId(id);
        // 收藏本课程的userid数组
        List<UserCourse> userCourses = userCourseMapper.selectByCourseId(id);
        Integer [] userid = new Integer[userCourses.size()];
        for (int i=0;i<userid.length;i++){
            userid[i]= Math.toIntExact(userCourses.get(i).getUserId());
        }
        course.setCoursemarkcount(count);
        course.setUsercoursecount(count2);
        course.setUserid(userid);
        return course;
    }
}
